define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'leave/index' + location.search,
                    add_url: 'leave/add',
                    edit_url: 'leave/edit',
                    del_url: 'leave/del',
                    multi_url: 'leave/multi',
                    import_url: 'leave/import',
                    table: 'leave',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'information.name', title: '请假人', operate: 'LIKE'},
                        {field: 'admin.nickname', title: '申请人', operate: 'LIKE'},
                        //{field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        // {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'new_start', title: '请假时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter:function(value,row,index){return value + " 到 " + row.new_end; }},
                        {field: 'day', title: __('Day'),formatter:function(value,row,index){return value + "小时"  }},
                        {field: 'state', title: __('State'), searchList: {"0":__('State 0'),"1":__('State 1'),"2":__('State 2')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on('blur','#c-end_time',function () {

                    var startTime = $('#c-start_time').val();
                    var endTime = $(this).val();
                    if(endTime<startTime)
                    {
                        alert('结束时间不能小于开始时间');
                        return;
                    }

                    if(startTime.length>0 && endTime.length>0)
                    {

                        var diff = datedifference(startTime,endTime);
                        $('#c-day').val(diff);
                    }


                })

                function datedifference(sDate1, sDate2) {    //sDate1和sDate2是2006-12-18格式
                    var d1 = new Date(sDate1);
                    var d2 = new Date(sDate2);
                    //console.log(parseInt(d2 - d1));//两个时间相差的毫秒数
                    //console.log(parseInt(d2 - d1) / 1000);//两个时间相差的秒数
                    //console.log(parseInt(d2 - d1) / 1000 / 60);//两个时间相差的分钟数
                    //console.log(parseInt(d2 - d1) / 1000 / 3600);//两个时间相差的小时数
                    return parseInt(d2 - d1)/1000/3600;
                };
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
